/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package org.aroundme.unittests;

import java.io.UnsupportedEncodingException;

import org.aroundme.controller.HttpRequestInterceptor;
import org.aroundme.model.CategoryName;
import org.aroundme.unitTools.TestFixture;
import org.aroundme.utility.Constants;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class HttpRequestInterceptorTest extends TestFixture {
	private HttpRequestInterceptor requestInterceptor;
	private MockHttpServletRequest mockRequest;
	private MockHttpServletResponse mockResponse;
	
	protected void setUp() throws Exception {
		super.setUp();
		requestInterceptor = (HttpRequestInterceptor) springFactory.getBean(HttpRequestInterceptor.class.getSimpleName());
		mockRequest = new MockHttpServletRequest("POST", "/");
		mockRequest.setContentType("application/octet-stream");
		mockResponse = new MockHttpServletResponse();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		requestInterceptor = null;
		mockRequest = null;
		mockResponse = null;
	}

	public void testPreHandleHttpServletRequestHttpServletResponseObject() throws UnsupportedEncodingException {
		xmlTestUtils.saveDummyUsersToDB();
		
		String xmlString = xmlTestUtils.createXMLCommitString(Constants.COMMIT_PLACE, "fixed", new String[]{CategoryName.HOTELS.toString(), CategoryName.BARS.toString()});
		mockRequest.setContent(xmlString.getBytes());
		
		boolean passedSecurityTest = requestInterceptor.preHandle(mockRequest, mockResponse, null);
		
		assertTrue("test should be successful", passedSecurityTest);
		
		//change such that it doesn't succeed
		xmlTestUtils.getDummyUsers().get(0).setPassword("x");
		xmlString = xmlTestUtils.createXMLCommitString(Constants.COMMIT_PLACE, "fixed", new String[]{CategoryName.HOTELS.toString(), CategoryName.BARS.toString()});
		mockRequest.setContent(xmlString.getBytes());
		
		passedSecurityTest = requestInterceptor.preHandle(mockRequest, mockResponse, null);
		String response = mockResponse.getContentAsString();
		assertFalse(passedSecurityTest);		
		assertTrue("contains type alert", response.contains("<type>" + Constants.ALERT + "</type>"));
		assertTrue("should be error alert", response.contains("<level>" + Constants.ALERT_ERROR + "</level>"));
	}

}
